package jk.zmn.sorm.utils;


import jk.zmn.sorm.bean.ColumnInfo;
import jk.zmn.sorm.bean.Configuration;
import jk.zmn.sorm.bean.TableInfo;
import jk.zmn.sorm.bean.javaFeildInfo;
import jk.zmn.sorm.core.DBManager;
import jk.zmn.sorm.core.MysqlConvertorHandler;
import jk.zmn.sorm.core.TableContext;
import jk.zmn.sorm.core.TypeConvertorHandler;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * @author 张梦楠
 * @简书：https://www.jianshu.com/u/d611be10d1a6
 * @码云：https://gitee.com/zhangqiye/events
 * @Date: 2018/6/14
 *
 *  用于生成java源文件的工具类
 *
 **/
public class JavaFileUtils {

    private static Configuration configuration = DBManager.getConfiguration();
    /**
     * private String name;
     * <p>
     * private String dataType;
     * <p>
     * private int keyType;
     */
    public static javaFeildInfo createJavaFeild(ColumnInfo columnInfo, TypeConvertorHandler convertorHandler) {

        //将字段数据类型转换成java数据类型
        String javaType = convertorHandler.JdbcType2JavaType(columnInfo.getDataType());

        String columnName = columnInfo.getName().toLowerCase();
        javaFeildInfo feildInfo = new javaFeildInfo();
        //生成属性语句
        feildInfo.setFeildInfo("\tprivate " + javaType + " " + StringUtils.trimUnderLine(columnName) + ";\n");

        StringBuilder sb = new StringBuilder();
        sb.append("\tpublic " + javaType + " " + "get" + StringUtils.UpFirstString(columnName) + "() {\n");

        sb.append("\t\treturn " + columnName + ";\n");

        sb.append("\t}\n");

        feildInfo.setGetFeildInfo(sb.toString());


        StringBuilder sb1 = new StringBuilder();

        sb1.append("\tpublic void " + "set" + StringUtils.UpFirstString(columnName) + "(" + javaType + " " + columnName + ") {\n");

        sb1.append("\t\t this." + columnName + " = " + columnName + ";\n");

        sb1.append("\t}\n");

        feildInfo.setSetFeildInfo(sb1.toString());

        return feildInfo;
    }


    public static void createJavaFile(TableInfo tableInfo, TypeConvertorHandler typeConvertorHandler){

        //得到所有的列信息
        Map<String, ColumnInfo> columns = tableInfo.getColumns();

        ArrayList<javaFeildInfo> javaFeildInfos = new ArrayList<>();
        Collection<ColumnInfo> values = columns.values();

        //生成所有的java属性信息和get set方法
        for (ColumnInfo columnInfo : values){
            javaFeildInfo javaFeild = createJavaFeild(columnInfo, typeConvertorHandler);
            javaFeildInfos.add(javaFeild);
        }

        StringBuilder sb = new StringBuilder();

        sb.append("package "+configuration.getPackageName()+";\n\n");
        sb.append("import java.sql.*;\n");
        sb.append("import java.util.*;\n\n");
        sb.append("public class "+StringUtils.UpFirstString(tableInfo.getName())+" {\n\n");

        for (javaFeildInfo javaFeildInfo: javaFeildInfos){
            sb.append(javaFeildInfo.getFeildInfo());
        }
        sb.append("\n");
        for (javaFeildInfo javaFeildInfo: javaFeildInfos){
            sb.append(javaFeildInfo.getGetFeildInfo());
        }
        for (javaFeildInfo javaFeildInfo: javaFeildInfos){
            sb.append(javaFeildInfo.getSetFeildInfo());
        }

        sb.append("}\n");
        //System.out.println(sb.toString());

        String classInfo = sb.toString();

        String filePathFromPackage = PathUtils.getFilePathFromPackage(configuration.getPackageName());
        File file = new File(filePathFromPackage, StringUtils.UpFirstString(tableInfo.getName()) + ".java");
        BufferedOutputStream bufferedOutputStream=null;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));

            bufferedOutputStream.write(classInfo.getBytes(),0,classInfo.getBytes().length);

            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("表"+tableInfo.getName()+"对应的类"+StringUtils.UpFirstString(tableInfo.getName())+"已自动生成..");
    }


    public static void createJavaFileToPackage(){

        Map<String, TableInfo> tables = TableContext.tables;
        TypeConvertorHandler convertorHandler = null;
        if (configuration.getUseDB().equalsIgnoreCase("mysql")){
            convertorHandler = new MysqlConvertorHandler();
        }
        for (TableInfo tableInfo:tables.values()){
            createJavaFile((tableInfo),convertorHandler);
        }
    }


}
